<template>
  <el-container class="home-container">
    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '60px' : '200px'">
      <div style="height:64px;line-height:64px;text-align:center">
        <img
          v-if="isCollapse"
          src="../assets/elm.png"
          height="60%"
          alt=""
          style="margin-top:20%;border-radius: 50%"
        />
        <span v-else class="showSpan">饱了么 Admin</span>
      </div>
      <!-- 一级菜单 -->
      <el-menu
        background-color="#fff"
        text-color="#000"
        active-text-color="#409EFF"
        unique-opened
        :collapse="isCollapse"
        :collapse-transition="false"
        router
        :default-active="activePath"
      >
        <!-- 一级菜单 -->
        <el-submenu
          :index="item.id + ''"
          v-for="item in menuList"
          :key="item.id"
        >
          <template slot="title">
            <i :class="iconObj[item.id]"></i>
            <span>{{ item.authName }}</span>
          </template>
          <!-- 二级菜单 -->
          <el-menu-item
            :index="'/' + subItem.path"
            v-for="subItem in item.children"
            :key="subItem.id"
            @click="saveNavItem('/' + subItem.path)"
          >
            <i class="el-icon-menu"></i>
            <span>{{ subItem.authName }}</span>
          </el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>
    <!-- 主体 -->
    <el-container>
      <!-- 头部 -->
      <el-header>
        <i
          :class="['showI', isCollapse ? 'el-icon-s-fold' : 'el-icon-s-unfold']"
          circle
          @click="isCollapse = !isCollapse"
        ></i>
        <el-button
          type="text"
          @click="logout"
          size="small"
          icon="el-icon-switch-button"
          class="logoutBtn"
        />
      </el-header>
      <!-- 内容 -->
      <el-main
        :style="{
          left: isCollapse ? '60px' : '200px'
        }"
      >
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 菜单列表
      menuList: [],
      // 图标对象
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    /** 保存链接的激活状态 */
    saveNavItem(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
* {
  transition: all 0.3s;
}
.el-container,
.home-container {
  height: 100%;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
.el-header {
  // position: relative;
  // width: 100%;
  // height: 60px;
  background-color: #fff;
  color: #333;
  display: flex;
  justify-content: space-between;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
    font-size: 20px;
    > img {
      border-radius: 50%;
    }
    > span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  color: #333;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
}
.el-menu {
  border-right: 0;
}
.iconfont {
  margin-right: 10px;
}
.showSpan {
  color: #409eff;
  font-weight: 500;
  font-size: 18px;
}
.showI {
  display: block;
  box-sizing: border-box;
  width: 30px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  border-radius: 50%;
  // border: 1px solid transparent;
}
.showI:hover {
  color: #409eff;
  box-shadow: 0px 5px 12px 4px rgba(0, 34, 101, 0.04),
    0px 3px 6px 0px rgba(0, 34, 101, 0.04),
    0px 1px 2px -2px rgba(0, 34, 101, 0.04);
}
.logoutBtn {
  width: 30px;
  height: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 50%;
  font-weight: 700;
  font-size: 18px;
}
.logoutBtn:hover {
  box-shadow: 0px 5px 12px 4px rgba(0, 34, 101, 0.04),
    0px 3px 6px 0px rgba(0, 34, 101, 0.04),
    0px 1px 2px -2px rgba(0, 34, 101, 0.04);
}
</style>
